package com.toycloud.business.controller.demand;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import com.toycloud.business.domain.bo.DemandPanelBo;
import com.toycloud.business.domain.bo.DemandRecordBo;
import com.toycloud.business.domain.bo.DemandRecordPageBo;
import com.toycloud.business.domain.vo.*;
import com.toycloud.business.service.IDemandRecordService;
import com.toycloud.common.core.page.TableDataInfo;
import com.toycloud.common.core.utils.poi.RuoyiExcelUtil;
import com.toycloud.common.sso.api.RemoteSsoService;
import com.toycloud.common.utils.BeanCopyUtils;
import com.toycloud.system.service.business.IFeedbackApplicationVersionManagerDetailService;
import com.toycloud.system.utils.JwtUtil;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.toycloud.common.core.controller.BaseController;
import com.toycloud.common.core.domain.PageQuery;
import com.toycloud.common.core.domain.R;
import com.toycloud.common.core.validate.AddGroup;
import com.toycloud.common.core.validate.EditGroup;

import static com.toycloud.interceptor.HeaderInterceptor.APPID;


/**
 * demand
 *
 * @author binlu6
 * @date 2024-10-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demand/demandRecord")
public class DemandRecordController extends BaseController {

    private final IDemandRecordService iDemandRecordService;

    private final IFeedbackApplicationVersionManagerDetailService iFeedbackApplicationVersionManagerDetailService;

    /**
     * 查询demand列表
     */
//    @SaCheckPermission("demand:demandRecord:list")
    @PostMapping("/list")
    public TableDataInfo<DemandRecordVo> list(@RequestBody DemandRecordPageBo bo) {
        return iDemandRecordService.queryPageList(bo);
    }

    /**
     * 看板列表
     */
    @GetMapping(value = {"/panel", "/panel/{categoryType}"})
    public R<List<PanelDto>> list(@RequestParam(value = "categoryType", required = false) String categoryType){
        // 如果 categoryType 为空，提供默认值
        if (categoryType == null || categoryType.trim().isEmpty()) {
            categoryType = "all"; // 这里可以根据业务逻辑设定默认值
        }
        return R.ok(iDemandRecordService.queryPanel(categoryType));
    }

    @PostMapping(value = {"/panel"})
    public R<List<PanelDto>> list(@RequestBody DemandPanelBo bo){
        return R.ok(iDemandRecordService.queryPanelBybo(bo));
    }

    /**
     * 导出demand列表
     */
    @PostMapping("/export")
    public void export(@RequestBody DemandRecordBo bo, HttpServletResponse response) {
        List<DemandRecordVo> list = iDemandRecordService.queryList(bo);
        RuoyiExcelUtil.exportExcel(list, "需求记录", DemandRecordVo.class, response);
    }

    @PostMapping("/export/filter")
    public void exportfilter(@RequestBody DemandRecordBo bo, HttpServletResponse response) {
        List<DemandRecordVo> list = iDemandRecordService.queryList(bo);
        List<FilterVo> newList = new ArrayList<>();
        list.forEach(i->{
            FilterVo filterVo = new FilterVo();
            BeanCopyUtils.copy(i,filterVo);
            newList.add(filterVo);
        });
        RuoyiExcelUtil.exportExcel(newList, "需求记录", FilterVo.class, response);
    }

    @PostMapping("/export/survey")
    public void exportsurvey( @RequestBody DemandRecordBo bo, HttpServletResponse response) {
        List<DemandRecordVo> list = iDemandRecordService.queryList(bo);
        List<surveyVo> newList = new ArrayList<>();
        list.forEach(i->{
            surveyVo surveyVo = new surveyVo();
            BeanCopyUtils.copy(i,surveyVo);
            newList.add(surveyVo);
        });
        RuoyiExcelUtil.exportExcel(newList, "需求记录", surveyVo.class, response);
    }

    @PostMapping("/export/analysis")
    public void exportAnalysis(@RequestBody DemandRecordBo bo, HttpServletResponse response) {
        List<DemandRecordVo> list = iDemandRecordService.queryList(bo);
        List<AnalysisVo> newList = new ArrayList<>();
        list.forEach(i->{
            AnalysisVo analysisVo = new AnalysisVo();
            BeanCopyUtils.copy(i,analysisVo);
            newList.add(analysisVo);
        });
        RuoyiExcelUtil.exportExcel(newList, "需求记录", AnalysisVo.class, response);
    }

    @PostMapping("/export/review")
    public void exportReview(@RequestBody DemandRecordBo bo, HttpServletResponse response) {
        List<DemandRecordVo> list = iDemandRecordService.queryList(bo);
        List<ReviewVo> newList = new ArrayList<>();
        list.forEach(i->{
            ReviewVo reviewVo = new ReviewVo();
            BeanCopyUtils.copy(i,reviewVo);
            newList.add(reviewVo);
        });
        RuoyiExcelUtil.exportExcel(newList, "需求记录", ReviewVo.class, response);
    }

    @PostMapping("/export/design")
    public void exportDesign(@RequestBody DemandRecordBo bo, HttpServletResponse response) {
        List<DemandRecordVo> list = iDemandRecordService.queryList(bo);
        List<DesignVo> newList = new ArrayList<>();
        list.forEach(i->{
            DesignVo designVo = new DesignVo();
            BeanCopyUtils.copy(i,designVo);
            newList.add(designVo);
        });
        RuoyiExcelUtil.exportExcel(newList, "需求记录", DesignVo.class, response);
    }

    @PostMapping("/export/develop")
    public void exportDevelop(@RequestBody DemandRecordBo bo, HttpServletResponse response) {
        List<DemandRecordVo> list = iDemandRecordService.queryList(bo);
        List<DevelopVo> newList = new ArrayList<>();
        list.forEach(i->{
            DevelopVo developVo = new DevelopVo();
            BeanCopyUtils.copy(i,developVo);
            newList.add(developVo);
        });
        RuoyiExcelUtil.exportExcel(newList, "需求记录", DevelopVo.class, response);
    }

    @PostMapping("/export/release")
    public void exportRelease(@RequestBody DemandRecordBo bo, HttpServletResponse response) {
        List<DemandRecordVo> list = iDemandRecordService.queryList(bo);
        List<ReleaseVo> newList = new ArrayList<>();
        list.forEach(i->{
            ReleaseVo releaseVo = new ReleaseVo();
            BeanCopyUtils.copy(i,releaseVo);
            newList.add(releaseVo);
        });
        RuoyiExcelUtil.exportExcel(newList, "需求记录", ReleaseVo.class, response);
    }

    @PostMapping("/export/sale")
    public void exportSale(@RequestBody DemandRecordBo bo, HttpServletResponse response) {
        List<DemandRecordVo> list = iDemandRecordService.queryList(bo);
        List<SaleVo> newList = new ArrayList<>();
        list.forEach(i->{
            SaleVo saleVo = new SaleVo();
            BeanCopyUtils.copy(i,saleVo);
            newList.add(saleVo);
        });
        RuoyiExcelUtil.exportExcel(newList, "需求记录", SaleVo.class, response);
    }

    /**
     * 获取demand详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<DemandRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(iDemandRecordService.queryById(id));
    }

    /**
     * 变更版本需求
     *
     * @param id 主键
     */
    @GetMapping("/change/{id}")
    public R<Integer> changeInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(iDemandRecordService.changeById(id));
    }

    /**
     * 新增demand
     */
    @PostMapping()
    public R<Void> add(HttpServletRequest request, @Validated(AddGroup.class) @RequestBody DemandRecordBo bo) {
//        UserParam param =iDemandRecordService.username(request);
//        bo.setCreateBy(param.getUsername());
        return toAjax(iDemandRecordService.insertByBo(bo));
    }

    /**
     * 修改demand
     */
    @PutMapping()
    public R<Void> edit(HttpServletRequest request,@Validated(EditGroup.class) @RequestBody DemandRecordBo bo) {
//        UserParam param =iDemandRecordService.username(request);
//        bo.setUpdateBy(param.getUsername());
        return toAjax(iDemandRecordService.updateByBo(bo));
    }




    /**
     * 删除demand
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iDemandRecordService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @DeleteMapping("/deleteId/{id}")
    public R<Void> deletebyId(@PathVariable Long id){
        return  toAjax(iDemandRecordService.deleteById(id));
    }

    /**
     * 修改demand
     */
    @PutMapping("/updateVersion")
    public R<Void> editappVersion(String ids) {
        return toAjax(iDemandRecordService.updateVersion(ids));

    }
}
